package huxibianjie.com.lbscandroid.ui.new_activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import huxibianjie.com.lbscandroid.Constants;
import huxibianjie.com.lbscandroid.R;
import huxibianjie.com.lbscandroid.base.BaseActivity;
import huxibianjie.com.lbscandroid.event.ClickEvent;
import huxibianjie.com.lbscandroid.manager.UserManager;
import huxibianjie.com.lbscandroid.model.UserModel;
import huxibianjie.com.lbscandroid.task.Callback;
import huxibianjie.com.lbscandroid.task.Task;
import huxibianjie.com.lbscandroid.ui.Service;
import huxibianjie.com.lbscandroid.util.StringUtils;
import huxibianjie.com.lbscandroid.util.SystemUtils;

/**
 * 更换钱包地址
 */
public class UpdateWalletAddressActivity extends BaseActivity implements View.OnClickListener {

    private TextView tvCurrentAddress, tvNewAddress, tvInfo, tvOk;

    private String paste;

    @Override
    protected int getContentView() {
        return R.layout.activity_update_wallet_address;
    }

    @Override
    protected void initView() {
        initTitle(R.drawable.ic_back, "钱包绑定/更改");
        tvCurrentAddress = findViewById(R.id.tv_current_address);
        tvNewAddress = findViewById(R.id.tv_new_address);
        tvInfo = findViewById(R.id.tv_info);
        tvOk = findViewById(R.id.tv_ok);
    }

    @Override
    protected void initData() {
        if (UserManager.getInstance().isLogin()) {
            UserModel userModel = UserManager.getInstance().getUserModel();
            String purseAddress = userModel.getPurseAddress();
            if (purseAddress == null || purseAddress.length() < 42) {
                tvCurrentAddress.setText("您还未绑定钱包地址");
            } else {
                tvCurrentAddress.setText(userModel.getPurseAddress());
            }
        }
        String info = "注：官方唯一合作钱包为 ETHMOD，当前只支持使用该钱包进行提现。点击下载";
        SpannableString spannableString = new SpannableString(info);
        spannableString.setSpan(new Clickable(clickListener), info.lastIndexOf("。") + 1, info.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvInfo.setText(spannableString);
        tvInfo.setMovementMethod(LinkMovementMethod.getInstance());
    }

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SystemUtils.openBrowser(UpdateWalletAddressActivity.this, Constants.Wallet_Url);
        }
    };

    class Clickable extends ClickableSpan {
        private final View.OnClickListener mListener;

        public Clickable(View.OnClickListener l) {
            mListener = l;
        }

        @Override
        public void onClick(View v) {
            mListener.onClick(v);
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setUnderlineText(true);
        }
    }

    @Override
    protected void initListener() {
        tvNewAddress.setOnClickListener(this);
        tvOk.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_ok:
                if (!UserManager.getInstance().isLogin()) return;
                if (paste != null && paste.length() == 42) {
                    showLoading();
                    Task.create(Service.class).updatePurseAddress(String.valueOf(UserManager.getInstance().getUserModel().getId()), paste).enqueue(new Callback<String>() {
                        @Override
                        public void onSuccess(@Nullable String data) {
                            cancelLoading();
                            if (data != null) {
                                UserManager.getInstance().save(UserManager.getInstance().getUserModel().setPurseAddress(paste), false);
                                EventBus.getDefault().post(new ClickEvent(ClickEvent.Type.ADDRESS_UPDATE_SUCCESS, null, ""));
                                showToast("绑定成功");
                                finish();
                            } else {
                                new android.support.v7.app.AlertDialog.Builder(UpdateWalletAddressActivity.this).setMessage("绑定失败")
                                        .setPositiveButton("确定", null).show();
                            }
                        }

                        @Override
                        public void onFailure(@NonNull String error) {
                            cancelLoading();
                            new android.support.v7.app.AlertDialog.Builder(UpdateWalletAddressActivity.this).setMessage(error)
                                    .setPositiveButton("确定", null).show();
                        }
                    });

                } else {
                    showToast("您没有黏贴钱包地址或地址错误~");
                }
                break;
            case R.id.tv_new_address:
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                if (cm != null) {
                    try {
                        paste = (String) cm.getText();
                        if (!StringUtils.isEmpty(paste) && paste.length() == 42) {
                            tvNewAddress.setText(paste);
                        } else {
                            showToast("请您复制钱包地址后黏贴~");
                            tvNewAddress.setText("点击此处粘贴钱包地址");
                        }
                    } catch (Exception e) {
                        showToast("请您复制钱包地址后黏贴~");
                        tvNewAddress.setText("点击此处粘贴钱包地址");
                    }
                }
                break;
        }
    }
}
